﻿//========================================================= 
/**@file SocialCardAppAdapter.h
 * @brief 社保卡驱动
 * 
 * @date 2017-02-07   09:09:04
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_SOCIALCARDAPPADAPTER_H_
#define _LIBZHOUYB_SOCIALCARDAPPADAPTER_H_
//--------------------------------------------------------- 
#include "../base/ICCardCmd.h"
#include "../base/ICCardLibrary.h"
#include "../base/ICCardAppAdapter.h"
using namespace zhou_yb::device::iccard::base;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace device {
namespace iccard {
namespace social_security_card {
//--------------------------------------------------------- 
#define SYS_SSC_PSE "7378312E73682EC9E7BBE1B1A3D5CF"
//--------------------------------------------------------- 
/**
 * @brief 社保卡标准驱动
 * @date 20170207 09:53
 */
class SocialSecurityCardAppAdapter : public ICCardAppAdapter
{
protected:
    /// 发送缓冲区 
    ByteBuilder _sendBuff;
    /// 接收缓冲区 
    ByteBuilder _recvBuff;

    bool _select_aid(const ByteArray aid, SelectCmd::AidMode mode = SelectCmd::ByAid)
    {
        ByteBuilder cmd(8);
        ByteBuilder data(8);
        SelectCmd::Make(cmd, aid, SelectCmd::First, mode);
        _apdu(cmd, data);
        ASSERT_FuncErrInfo(_apdu(cmd, data), DeviceError::OperatorErr, "选文件失败");
        return true;
    }
public:
    bool GetCardNumber(ByteBuilder& cardNo)
    {
        LOG_FUNC_NAME();
        ASSERT_Device();

        /* 选文件 */
        ASSERT_FuncRet(_select_aid(DevCommand::FromAscii(SYS_SSC_PSE), SelectCmd::ByName));
        ASSERT_FuncRet(_select_aid(DevCommand::FromAscii("EF05")));

        ByteBuilder cmd(8);
        ByteBuilder data(8);
        ASSERT_FuncErrInfoRet(_apdu(DevCommand::FromAscii("00B207000B"), data), DeviceError::OperatorErr, "获取卡号失败");
        cardNo = data.SubArray(2);
        return _logRetValue(true);
    }
};
//--------------------------------------------------------- 
} // namespace social_security_card
} // namespace iccard
} // namespace device
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_SOCIALCARDAPPADAPTER_H_
//========================================================= 